// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Application</Project>
// <FileName>WalletAdminAppService.cs</FileName>
// <CreateTime>2017-05-16 17:19</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-05-16 17:19" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Vecen.BangChuang.BangChuang.DomainServices.Wallets;
using Vecen.BangChuang.BangChuang.Wallets.Dtos;

namespace Vecen.BangChuang.BangChuang.Wallets {

    /// <summary>
    /// 提现申请后台相关操作
    /// </summary>
    [AbpAuthorize()]
    public class WalletAdminAppService: BangChuangAppServiceBase, IWalletAdminAppService {
        private readonly WalletDomainService _walletDomainService;
        private readonly IRepository<PointWithdraw> _pointWithdrawRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="walletDomainService"></param>
        /// <param name="pointWithdrawRepository"></param>
        public WalletAdminAppService(WalletDomainService walletDomainService, IRepository<PointWithdraw> pointWithdrawRepository) {
            _walletDomainService = walletDomainService;
            _pointWithdrawRepository = pointWithdrawRepository;
        }

        /// <summary>
        /// 获得所有提现记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<WithdrawForAdminListOutDto>> GetWithdrawsAsync(
            GetAllWithdrawForAdminInputDto input) {
            var query = _pointWithdrawRepository.GetAll()
                .WhereIf(input.StartTime.HasValue, c => c.ApplyTime >= input.StartTime)
                .WhereIf(input.EndTime.HasValue, c => c.ApplyTime <= input.EndTime)
                .WhereIf(input.Status.HasValue, c => c.Status == input.Status);
            var total = await Task.Run(() => query.Count());
            var paged = await Task.Run(() => query.OrderBy(input.Sorting).PageBy(input).ToList());
            return new PagedResultDto<WithdrawForAdminListOutDto>(total,
                paged.MapTo<List<WithdrawForAdminListOutDto>>());
        }

        /// <summary>
        /// 同意提现操作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task AgreeWithdrawApplyAsync(int id) {
            await _walletDomainService.AuditWithdrawApplyAsync(id,PointWithdrawStatus.Agreed);
        }

        /// <summary>
        /// 拒绝提现操作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task RejectWithdrawApplyAsync(int id) {
            await _walletDomainService.AuditWithdrawApplyAsync(id, PointWithdrawStatus.Rejected);
        }
    }
}